//
//  LXBigImageView.m
//  ZuLinProject
//
//  Created by bancheng on 2022/7/11.
//  Copyright © 2022 LX_App. All rights reserved.
//

#import "LXBigImageView.h"
#define MaxSCale 2.5  //最大缩放比例
#define MinScale 0.5  //最小缩放比例

@interface LXBigImageView()<UIScrollViewDelegate>

@property (nonatomic,strong) NSArray *imgS;
@property (nonatomic,assign) CGFloat totalScale;
@end

@implementation LXBigImageView

- (instancetype)initWithFrame:(CGRect)frame WithImageAry:(NSMutableArray *)imgArys{
    self = [super initWithFrame:frame];
    self.imgS = imgArys;
    if (self) {
        
        self.userInteractionEnabled=YES;
        //点击手势
        [self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(disMissView:)]];
        

        self.totalScale = 1.0;
        
        [self addSubview:self.scrollView];
       


    }
    return self;
}

- (void)setImgArys:(NSMutableArray *)imgArys{
    _imgArys = imgArys;
    
    
    [self.scrollView removeAllSubviews];
    
    [self setImageViewWithData];
    
}

-(void)setImageViewWithData{
    
    for (int i = 0; i < self.imgArys.count; i++) {
        UIImageView *bigImage = [[UIImageView alloc] initWithFrame:CGRectMake(i * self.width, 0, ScreenWidth,ScreenHeight)];
        YLUploadMedia *media = self.imgArys[i];
        bigImage.tag = i + 100;
        bigImage.image = (UIImage *)media.obj;
        bigImage.contentMode = UIViewContentModeScaleAspectFit;
        [self.scrollView addSubview:bigImage];
        
        bigImage.userInteractionEnabled=YES;
        
        
        //放大缩小手势
        UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];
        [bigImage addGestureRecognizer:pinch];

        if (self.isHidden == NO) {
            UIButton *delBtn = [[UIButton alloc] initWithFrame:CGRectMake(bigImage.X_width - 75, KStatuesBarHeight, 60, 60)];
            [delBtn setImage:[UIImage imageNamed:@"删除合同"] forState:UIControlStateNormal];
            [delBtn.imageView sizeToFit];
            [delBtn whenTapped:^{
                [self.imgArys removeObjectAtIndex:i];
                self.scrollView.contentSize = CGSizeMake(ScreenWidth * self.imgArys.count, 0);
                
                if (i==0) {
                    [self.scrollView setContentOffset:CGPointMake(0, 0)];
                }
                else{
                    [self.scrollView setContentOffset:CGPointMake((i - 1) * self.width, 0)];
                }
                if ([self.delegate respondsToSelector:@selector(reloadDataWithTableView)]) {
                    [self.delegate reloadDataWithTableView];
                }
                if (self.imgArys.count==0) {
                    [self removeAllSubviews];
                }
                
            }];
            [self.scrollView addSubview:delBtn];
            
        }
    }
    
    
}
- (UIScrollView *)scrollView {
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
        _scrollView.backgroundColor = [UIColor blackColor];
        _scrollView.contentSize = CGSizeMake(ScreenWidth * self.imgS.count, 0);
        _scrollView.pagingEnabled = YES;
        _scrollView.bounces = NO;
        _scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        _scrollView.delegate = self;
    }
    return _scrollView;
}
#pragma mark----放大缩小手势、拖动手势、点击手势
- (void)pinch:(UIPinchGestureRecognizer *)recognizer{
    
    CGFloat scale = recognizer.scale;
    
    //放大情况
    if(scale > 1.0){
        if(self.totalScale > MaxSCale) return;
    }
    
    //缩小情况
    if (scale < 1.0) {
        if (self.totalScale < MinScale) return;
    }
    
    recognizer.view.transform = CGAffineTransformScale(recognizer.view.transform, scale, scale);
    self.totalScale *= scale;
    recognizer.scale = 1.0;
    
}
- (void)changePoint:(UIPanGestureRecognizer *)sender {
       UIView *view = sender.view;
       if (sender.state == UIGestureRecognizerStateBegan || sender.state == UIGestureRecognizerStateChanged) {
             CGPoint translation = [sender translationInView:view.superview];
        
           [view setCenter:CGPointMake(view.centerX+translation.x, view.centerY+translation.y)];
           
               [sender setTranslation:CGPointZero inView:view.superview];
          
       }
    }
-(void)disMissView:(UITapGestureRecognizer *)tap{
    
//    CGPoint point = [tap locationInView:self];
    [self removeFromSuperview];
}


- (instancetype)initWithFrame:(CGRect)frame WithShowImageAry:(NSArray *)imagArys{
    self.imgS = [imagArys mutableCopy];
    self = [super initWithFrame:frame];
    if (self) {
        
        
        self.userInteractionEnabled=YES;
        //点击手势
        [self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(disMissView:)]];
        

        self.totalScale = 1.0;
        
        [self addSubview:self.scrollView];
        
        
        for (int i = 0; i < imagArys.count; i++) {
            
            NSString *url = imagArys[i];
            
            UIImageView *bigImage = [[UIImageView alloc] initWithFrame:CGRectMake(i * self.width, 0, ScreenWidth,ScreenHeight)];
            if ([url hasPrefix:@"http"]) {
                [bigImage sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:Image(@"暂无图片")];
            }
            else {
                [bigImage sd_setImageWithURL:[NSURL URLWithString:[HTTP_URL stringByAppendingString:url]] placeholderImage:Image(@"暂无图片")];
            }
            bigImage.contentMode = UIViewContentModeScaleAspectFit;
            [self.scrollView addSubview:bigImage];
            
            bigImage.userInteractionEnabled=YES;
            
            //放大缩小手势
            UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];
            [bigImage addGestureRecognizer:pinch];

        }
        
        
       


    }
    return self;
    
    
    
    
}

@end
